Cognitive Brownbag

Holly Zaharchuk

February 19, 2020

What is R Markdown?

  • Markup language
  • R Markdown != R

Parts of a Markdown document

  1. YAML header
  2. Markdown language
  3. Code chunks

YAML header

Global options for outputs/formatting

YAML example

Markdown

Plain text formatting allows for conversion to multiple document types

Markdown example

Chunks

# This is a chunk of R code that adds an image
knitr::include_graphics("images/example_chunk.png")

Creating output

Output types

  • PDF: requires \(\LaTeX\) to compile (see TinyTeX distribution)
  • HTML
  • Microsoft Office (Word/Powerpoint)

YAML specification

  • Use output argument in YAML header
  • Pay attention to indentation and colons
  • Press knit button or command/CTRL-shift-K

Render

  • Render file in console: rmarkdown::render(file, output_format)
  • Option to create PDF from HTML: pagedown::chrome_print(file)

Using templates

  1. Built-in templates
  2. Templates from R packages
  3. User-defined templates

Built-in templates

  • Presentations
    • ioslides and Slidy for HTML
    • Beamer for PDF
  • Shiny documents and presentations (interactive)

R packages

User-defined templates

\(\LaTeX\) templates LaTeX example

User-defined templates

Word Document Word example

Formatting

  1. YAML parameters and references
  2. \(\LaTeX\) and CSS code in Markdown
  3. Template documents

YAML parameters

YAML references

These files go in the same place as your .Rmd

  • Bibliography: .bib (I use BibDesk for my reference manager)
  • Bibiography style: .csl (see my CV and Psychonomics poster repos for APA 6 files)
  • \(\LaTeX\) styling: .cls
  • HTML styling: .css
  • Interacting with pandoc: .lua (multiple bibliographies)

\(\LaTeX\) and CSS

  • \(\LaTeX\) with PDFs
    • Packages
    • Symbols: here and here
    • Type-setting
  • CSS with HTML

\(\LaTeX\)

Examples from my stats homework

Packages Calls

\(\LaTeX\)

LaTeX example output

CSS/HTML

Examples from my Psychonomics poster

CSS formatting example HTML formatting example

Updating template documents

# Make dataframe with installed packages
pkgs <- installed.packages() %>%
  as.data.frame()

# Pull posterdown package 
pstr <- pkgs %>% 
  select(Package, LibPath, Version, Depends, Imports) %>%
  dplyr::filter(Package == "posterdown")

# Make table
kable(pstr) %>%
  kable_styling(bootstrap_options = "condensed", 
                font_size = 18)
Package LibPath Version Depends Imports
posterdown /Library/Frameworks/R.framework/Versions/3.5/Resources/library 1.0 NA pagedown, rmarkdown, yaml

Updating template documents

  • Save the original and move to a different location
  • Make one change at a time
  • Name the updated template with the same name in the same place as the original

Trouble-shooting

Identifying issues

  • Warnings vs. errors
  • Console vs. chunk
  • Markdown environment vs. R environment
  • When to use package specification ::

Errors

  • Chunk error: Error example
  • Markdown/YAML error: Error example

Tips

  • Run all chunks individually before compiling
  • Clear all variables: rm(ls = list())
  • Restart R environment: control/CTRL + fn + shift + F10

Helpful reference documents

Random tidbits

R Stuff

  • Regular expressions for working with free response text (cheat sheet here)
  • assign() function for dynamic variable names
  • Store ggplot parameters in a list()
  • source()
  • %notin%

Keys to success

  • Data are read only
  • Comment your code excessively
  • Keep chunks small